App({

  globalData: {
    // https://xiaochengxu.printeasy.top/prod-api/
    // https://scw.zhiqiyun.com/prod-api/
    URL: 'https://xiaochengxu.printeasy.top/prod-api/',
   // URL: 'https://scw.zhiqiyun.com/prod-api/', //设置全局请求URL
    openId: wx.getStorageSync('openId'),
    'appid': "wxc92e328fe669df24",
    'secret': "d6729ab1e4e7ba1f7c1e0539e86e40db",
    sessionKey: wx.getStorageSync('sessionKey'),
    // wx.getStorageSync("token")
    token: '',
    userInfo: wx.getStorageInfoSync('userInfo'),
    share: false, // 分享默认为false
    height: 0,
    qurd: '',
    jqm: ''
  },
  /**
   * 封装wx.request请求
   * method： 请求方式
   * url: 请求地址  
   * data： 要传递的参数
   * callback： 请求成功回调函数
   * errFun： 请求失败回调函数
   **/
  wxRequest(method, url, data, callback, errFun) {
    // console.log(method, url, data, callback, errFun);
    wx.request({
      url: url,
      method: method,
      data: data,
      header: {
        'content-type': method == 'GET' ? 'application/json' : 'application/json',
        'Accept': 'application/json',
        'Authorization': this.globalData.token
      },
      dataType: 'json',
      success: function (res) {
        callback(res.data);
      },
      fail: function (err) {
        // errFun(err);
        console.log(err);
      }
    })
  },
  //token过期重新获取
  getTokenFromServer(callback) {
    var that = this;
    wx.login({
      success: res => {
        let code = res.code;
        let url = that.globalData.URL + 'wx/openId/get/'
        if (code) {
          wx.request({
            url: url + code,
            method: 'POST',
            // data:{
            //   code:code
            // },
            header: {
              'content-type': 'application/json' // 默认值
            },
            success: function (res) {
              that.globalData.sessionKey = res.data.sessionKey;
              that.globalData.openId = res.data.openId;
              wx.getUserInfo({
                lang: "zh_CN",
                success: res => {
                  let avatar = res.userInfo.avatarUrl;
                  let nickName = res.userInfo.nickName;
                  let sex = JSON.stringify(res.userInfo.gender);
                  let openId = that.globalData.openId;
                  let sessionKey = that.globalData.sessionKey;
                  wx.request({
                    url: that.globalData.URL + 'wx/login',
                    method: 'POST',
                    data: {
                      avatar: avatar,
                      nickName: nickName,
                      openId: openId,
                      sessionKey: sessionKey,
                      sex: sex
                    },
                    header: {
                      "content-type": "application/x-www-form-urlencoded",
                      'content-type': 'application/json'
                    },
                    success: function (res) {
                      console.log(res)
                      wx.setStorageSync('token', res.data.data.token);
                    }
                  })
                }
              })

            }
          })
        }
      }
    })


  },
  onLaunch: function (options) {
    let that = this;
    //获取设备顶部窗口的高度（不同设备窗口高度不一样，根据这个来设置自定义导航栏的高度）
    wx.getSystemInfo({
      success: (res) => {
        this.globalData.height = res.statusBarHeight
      }
    })
  },
})